#include "loghelper.h"


QMutex LogLock;

loghelper* loghelper::Instance=0;

loghelper* loghelper::getInstance()
{
    if(Instance==0)
        Instance =new loghelper();
    return Instance;
}
loghelper::loghelper(QObject *parent): QObject(parent)
{
    QDateTime currentTime = QDateTime::currentDateTime();
    QString date= currentTime.toString("yyyyMMdd");
#ifdef Q_OS_ANDROID
    savePath="/storage/emulated/0/Download/"+date;
#else
    savePath="Logs/"+date;
#endif
}

//
loghelper::~loghelper()
{
}

void loghelper::CheckLogFile(){


    QDir dir;
    if (!(dir.exists(savePath)))
        dir.mkpath(savePath);
}

void loghelper:: WriteLog(QString type, QString content)
{
    CheckLogFile();
    LogLock.lock();
    QTime current_time = QTime::currentTime();
    QString fileName = savePath +"/"+ type + "_" +QString::number(current_time.hour())+".log";
    QDateTime currentTime = QDateTime::currentDateTime();
    QString time = currentTime.toString("yyyy-MM-dd hh:mm:ss");
    QString data = "[" + time + "]" + " \n" + content;

    QFile f(fileName);
    f.open(QIODevice::WriteOnly|QIODevice::Append) ;
    QTextStream t(&f);
    t<<data;
    f.close();

    LogLock.unlock();
}
